package com.szxydb.dmp.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.szxydb.dmp.admin.system.dao.SystemAttachmentDao;
import com.szxydb.dmp.admin.system.model.SystemAttachment;
import com.szxydb.dmp.admin.system.service.SystemAttachmentService;
import com.szxydb.dmp.admin.system.service.SystemConfigService;
import com.szxydb.dmp.admin.upload.service.AsyncService;
import com.szxydb.dmp.admin.upload.vo.FileResultVo;
import com.szxydb.dmp.common.PageParamRequest;
import com.szxydb.dmp.constants.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SystemAttachmentServiceImpl extends ServiceImpl<SystemAttachmentDao, SystemAttachment> implements SystemAttachmentService {
    @Resource
    private SystemAttachmentDao dao;

    @Autowired
    private AsyncService asyncService;

    @Autowired
    private SystemConfigService systemConfigService;

    @Override
    public void create(FileResultVo file, Integer pid) {
        SystemAttachment systemAttachment = new SystemAttachment();
        systemAttachment.setName(file.getFileName());
        systemAttachment.setAttDir(file.getUrl());
        systemAttachment.setSattDir(file.getUrl());
        systemAttachment.setAttSize(file.getFileSize().toString());
        systemAttachment.setAttType(file.getType());
        systemAttachment.setImageType(1);
        systemAttachment.setAttDir(file.getServerPath());
        systemAttachment.setPid(pid);
        save(systemAttachment);
    }

    @Override
    public void async() {

        String uploadType = systemConfigService.getValueByKeyException("uploadType");
        if (Integer.parseInt(uploadType) <= 1){
            return;
        }

        List<SystemAttachment> asyncList = getAsyncList();
        if (asyncList.size() <1){
            return;
        }
        asyncService.async(asyncList);
    }

    @Override
    public void updateCloudType(Integer attId, int type) {
        SystemAttachment systemAttachment = new SystemAttachment();
        systemAttachment.setAttId(attId);
        systemAttachment.setImageType(type);
        updateById(systemAttachment);
    }

    @Override
    public List<SystemAttachment> getList(Integer pid, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(),pageParamRequest.getLimit());
        LambdaQueryWrapper<SystemAttachment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemAttachment::getPid,pid);
        lambdaQueryWrapper.orderByDesc(SystemAttachment::getAttId);
        return dao.selectList(lambdaQueryWrapper);
    }

    @Override
    public String prefixImage(String path) {
        return path.replace("image/",getCdnUrl()+"/image/");
    }

    public String getCdnUrl(){
        return asyncService.getCurrentBaseUrl();
    }

    @Override
    public String prefixFile(String path) {
        return path.replace("file/",getCdnUrl()+"/image/");
    }

    @Override
    public String clearPrefix(String path) {
        if (StringUtils.isBlank(path)){
            return path;
        }
        Constants.CND_URL = asyncService.getCurrentBaseUrl();
        if (path.contains(getCdnUrl() +"/")){
            return path.replace(getCdnUrl()+"/","");
        }
        return path;
    }

    @Override
    public List<SystemAttachment> getByEntity(SystemAttachment systemAttachment) {
        LambdaQueryWrapper<SystemAttachment> lqw = new LambdaQueryWrapper<>();
        lqw.setEntity(systemAttachment);
        return dao.selectList(lqw);
    }

    private List<SystemAttachment> getAsyncList(){
        PageHelper.startPage(Constants.DEFAULT_PAGE,Constants.DEFAULT_LIMIT);
        LambdaQueryWrapper<SystemAttachment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemAttachment::getImageType,1);
        lambdaQueryWrapper.isNotNull(SystemAttachment::getAttDir);
        lambdaQueryWrapper.orderByDesc(SystemAttachment::getAttId);
        return dao.selectList(lambdaQueryWrapper);
    }
}
